package com.liu.controller;

import com.liu.domain.health;
import com.liu.domain.student;
import com.liu.repository.HealthRepository;
import com.liu.repository.JiFenRepository;
import com.liu.utils.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by liu on 17-9-22.
 */
@RestController
public class HealthController {
    private final static Logger logger = LoggerFactory.getLogger(HealthController.class);

    @Autowired
    private HealthRepository healthRepository;

    @Autowired
    JiFenRepository jiFenRepository;
    /*
    查询所有列表
     */
    @GetMapping(value = "/health")
    public String healthList(String searchHea,String sj,String rid,String rank,HttpServletRequest request) throws ParseException {
      /*  System.out.println(searchHea);
        System.out.println(rid);
        System.out.println(rank);
        System.out.println(sj);*/

        if (sj != null && sj != ""){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            Date date = sdf.parse(sj);
            System.out.println(date);
            List<health> heas = healthRepository.findbytime(date);
            for (health r: heas) {
                int health = r.getR_id();
                String r_id = String.valueOf(health);
                String buildId = r_id.substring(0, 2);
                r_id = r_id.substring(2, 5);
                r.setRoom_id(buildId + "#" + r_id);
            }
            return "{\"code\":0,\"msg\":\"\",\"count\":" + heas.size() + ",\"data\":" + heas + "}";
        }else if (rid !=null && rid != "" ){
            Integer i;
            i = Integer.valueOf(rid);
            List<health> healthList = healthRepository.findbyroom(i);
            for (health r: healthList) {
                int health = r.getR_id();
                String r_id = String.valueOf(health);
                String buildId = r_id.substring(0, 2);
                r_id = r_id.substring(2, 5);
                r.setRoom_id(buildId + "#" + r_id);
            }
            return "{\"code\":0,\"msg\":\"\",\"count\":" + healthList.size() + ",\"data\":" + healthList + "}";

        }else if (rank !=null && rank != "" ){

            List<health> healthList = healthRepository.findAllByRank(rank);
            for (health r: healthList) {
                int health = r.getR_id();
                String r_id = String.valueOf(health);
                String buildId = r_id.substring(0, 2);
                r_id = r_id.substring(2, 5);
                r.setRoom_id(buildId + "#" + r_id);
            }
            return "{\"code\":0,\"msg\":\"\",\"count\":" + healthList.size() + ",\"data\":" + healthList + "}";
        }

        else {
            List<health> healths = healthRepository.findAll();
            for (health r: healths) {
                int health = r.getR_id();
                String r_id = String.valueOf(health);
                String buildId = r_id.substring(0, 2);
                r_id = r_id.substring(2, 5);
                r.setRoom_id(buildId + "#" + r_id);
            }
            return "{\"code\":0,\"msg\":\"\",\"count\":" + healths.size() + ",\"data\":" + healthRepository.findAll() + "}";
        }
    }
    /*
       查询rank优最多
        */
    @GetMapping(value = "/healthg")
    public List<health> healthList1() {
        List<health> healths = healthRepository.findbygood();
        for (health r: healths) {
            int health = r.getR_id();
            String r_id = String.valueOf(health);
            String buildId = r_id.substring(0, 2);
            r_id = r_id.substring(2, 5);
            r.setRoom_id(buildId + "#" + r_id);
        }
        return healths;
    }
    /*
       查询rank差最多
        */
    @GetMapping(value = "/healthb")
    public List<health> healthList2() {
        List<health> healths = healthRepository.findbybad();
        for (health r: healths) {
            int health = r.getR_id();
            String r_id = String.valueOf(health);
            String buildId = r_id.substring(0, 2);
            r_id = r_id.substring(2, 5);
            r.setRoom_id(buildId + "#" + r_id);
        }
        return healths;
    }
    /*
    添加
     */
    @PostMapping(value = "/health")
    public ModelAndView HEA(health vt) {
        healthRepository.save(vt);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("addHea");
        mav.addObject("flag", 1);
        System.out.println("flag");
        return mav;
    }

    // ajax方式添加评分
    @RequestMapping("/addHealth")
    public String addHealth(int id, String rank, Date date){
        health vt = new health();
        vt.setDate(date);
        vt.setRank(rank);
        vt.setR_id(id);

        int decrease = 0;
        if("优".equals(rank)){
            decrease = 2;
        }
        if("良".equals(rank)){
            decrease = 1;
        }
        if("中".equals(rank)){
            decrease = 0;
        }
        if("差".equals(rank)){
            decrease = -2;
        }
//        if("违规".equals(rank)){
//            decrease = -3;
//        }
        int[] time = TimeUtil.dealDateToSchool(date);
        jiFenRepository.modifyRoomScore(id, decrease, time[0], time[1]);

        System.out.println("----------"+id+":"+date);
        SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd");
        List<health> result = healthRepository.findAllByR_idAndDate(id, smf.format(date));
        if(result.size()>0){
            return "Already";
        }
        return healthRepository.save(vt)+"";
    }

    /*
    更新一个
     */
    @PutMapping(value = "/health/{id}")
    public health healthUpdate(@PathVariable("id") Integer id, health health) {

        return healthRepository.save(health);
    }
    /*
    删除一个
     */

    @DeleteMapping(value = "/health/{id}")
    public void healthDelete(@PathVariable("id") Integer id){
        healthRepository.delete(id);
    }


    //学生查看 本寝卫生
    @RequestMapping("/getRoomHea")
    public List<health> getClassStu(HttpSession session, String t) {
        student stu = (student) session.getAttribute("user");
        List<health> heas= healthRepository.findbyroom(stu.getRoom_id());
        return  heas;

    }

}
